package airAgent.group2;

import java.util.HashMap;
import java.util.Random;

import airAgent.group2.util.Util;

import ontology.ontology.AirAgentOntology;
import ontology.ontology.InformDate;

import jade.content.Concept;
import jade.content.ContentElement;
import jade.content.lang.Codec;
import jade.content.lang.Codec.CodecException;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.Ontology;
import jade.content.onto.OntologyException;
import jade.content.onto.UngroundedException;
import jade.content.onto.basic.Action;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

public class ClientGenerator extends Agent{

	private static final long serialVersionUID = 1L;
	public static HashMap<String, Boolean> cancellation = new HashMap<String, Boolean>();
	private static Codec codec = new SLCodec();
    static Ontology ontology = AirAgentOntology.getInstance();
	
	
	protected void setup(){
		System.out.println("client generator: enters into the system");
		getContentManager().registerLanguage(codec);
		getContentManager().registerOntology(ontology);
		
		Util.addTimerBehavior(this);


		addBehaviour(new CyclicBehaviour(this) {
			private static final long serialVersionUID = 1L;
			public void action() {
				ACLMessage msg = receive (MessageTemplate.MatchLanguage(codec.getName()));
				if (msg != null) {
					if (msg.getPerformative() == ACLMessage.INFORM) {
						ContentElement ce = null;
						try {
							ce = getContentManager().extractContent(msg);
						} catch (UngroundedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (CodecException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (OntologyException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
                        Concept conc = ((Action)ce).getAction();
                        if(conc instanceof InformDate){
                        	int date = ((InformDate)conc).getDate();
                        	String client = "airAgent.group2.Client";
                    		for(int i = 0; i < (int)(Util.getRandom(Util.CLIENTS_PER_DAY, Util.CLIENTS_VARIANCE)); i++){
                    			cancellation.put("C" + date + "-" + i, new Random().nextBoolean());
                    			Simulation2.startAgent(myAgent, "C" + date + "-" + i, client);
                    		}
                        }
						
					}
				}
			}
		});
	}


}
